package com.quanshu.exchange.support.utils;

import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * RxJava防抖操作
 */
public class RxViewHelper {

    private int doubleTimeout = 200;//双击延时

    private volatile static RxViewHelper instance;

    public static RxViewHelper getInstance() {
        if (instance == null) {
            synchronized (RxViewHelper.class) {
                if (instance == null) {
                    instance = new RxViewHelper();
                }
            }

        }
        return instance;
    }

    private RxViewHelper() {

    }

    /**
     * 点击事件，无防抖
     *
     * @param view
     * @param action
     */
    public void sampleClicks(View view, Consumer<? super Object> action) {
        RxView.clicks(view)
                .subscribe(action);
    }

    /**
     * 点击事件,有防抖
     *
     * @param view
     * @param action
     */
    public void clicks(View view, Consumer<? super Object> action) {
        //两秒钟之内只取一个点击事件，防抖操作
        RxView.clicks(view)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(action);
    }

    /**
     * 点击事件,有防抖
     *
     * @param view
     * @param action
     */
    public void clicks(View view, int timeSeconds, Consumer<? super Object> action) {
        //两秒钟之内只取一个点击事件，防抖操作
        RxView.clicks(view)
                .throttleFirst(timeSeconds, TimeUnit.SECONDS)
                .subscribe(action);
    }

    /**
     * 长按事件
     *
     * @param view
     * @param action
     */
    public void longClicks(View view, Consumer<? super Object> action) {
        RxView.longClicks(view)
                .subscribe(action);
    }

    /**
     * 双击事件
     *
     * @param view
     * @param action
     */
    public void doubleClicks(View view, Consumer<? super List<Object>> action) {
        Observable<Object> observable = RxView.clicks(view).share();
        observable.buffer(observable.debounce(doubleTimeout, TimeUnit.MILLISECONDS))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(action);
    }

    /**
     * textview 监听
     *
     * @param view
     * @param action
     */
    public void afterTextChangeEvents(TextView view, Consumer<TextViewAfterTextChangeEvent> action) {
        RxTextView.afterTextChangeEvents(view)
                .subscribe(action);
    }

    public void textChangeEvents(TextView view, Consumer<TextViewTextChangeEvent> action) {
        RxTextView.textChangeEvents(view)
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(action);
    }

}
